<template>
<div class="welisten-talk-create">
	<Row>
		<Col :span="24">
			<Breadcrumb>
				<BreadcrumbItem to="/" @click.native="changeMenu(0)"><Icon type="home"></Icon> 首页</BreadcrumbItem>
				<BreadcrumbItem :to="{'name':'welisten-talk-index'}">说说管理</BreadcrumbItem>
				<BreadcrumbItem>{{title}}</BreadcrumbItem>
			</Breadcrumb>
			<div class="cleaner20"></div>
		</Col>
	</Row>
	<Form ref="vform" :model="vform" :label-width="100"  :rules="ruleValidate">
		<Row type="flex" :gutter="16">
			<Col :span=12>
				<Row type="flex">
					<Col :span="24">
						<FormItem ref="realname" label="昵称" prop="nickname">
							<Input v-model="vform.nickname" placeholder="请填写昵称" readonly>
								<Icon type="md-search" slot="suffix" @click.native="onSelectUser"></Icon>
							</Input>
						</FormItem>
					</Col>
					<Col :span="24">
						<FormItem label="状态" prop="status">
							<Select v-model="vform.status" >
								<Option v-for="(item,index) in statusList" :value="item.value" :key="index">{{ item.label }}</Option>
							</Select>
						</FormItem>
					</Col>
					<Col :span="24">
						<FormItem label="权重" prop="sequence">
							<InputNumber :max="9999" :min="1" :step="1" v-model="vform.sequence"></InputNumber>
						</FormItem>
					</Col>
					<Col :span="24">
						<FormItem label="备注">
							<Input v-model="vform.memo"></Input>
						</FormItem>
					</Col>
				</Row>
			</Col>

			<Col :span="24">
				<FormItem label="说说">
					<quill-editor ref="quill1" v-model="vform.content" :options="editorOption"></quill-editor>
				</FormItem>
			</Col>
			<Col :span="24">
				<FormItem label="">
					<Button type="success" @click="onSave">保存</Button>
				</FormItem>
			</Col>
		</Row>
	</Form>

	<Listener-Selector  :show.sync="showBox" @on-hide="showBox=false" @on-change="onListenerChange"></Listener-Selector>
	

</div>
</template>
<script>
// 引入子组件
import hljs from 'highlight.js'
import 'highlight.js/styles/vs2015.css'

// quill编辑器的字体
var fonts = ['SimSun', 'Microsoft-YaHei','Arial','Times-New-Roman','sans-serif'];  
var Font = Quill.import('formats/font'); 
Font.whitelist = fonts; //将字体加入到白名单 
Quill.register(Font, true);

// 引入子组件
import ListenerSelector from '@/components/ListenerSelector/ListenerSelector.vue'
import {mapState,mapGetters} from 'vuex'
import { Tag, Card, Page, Modal, Spin, Row, Col, ButtonGroup, Button,  Breadcrumb, BreadcrumbItem, Icon, Form, FormItem, Input, Select, Option, DatePicker, InputNumber } from 'iview';
export default { 
	name:'welisten-talk-create',
	components: { Tag, Card, Page, Modal, ListenerSelector, Spin, Row, Col, ButtonGroup, Button, Breadcrumb, BreadcrumbItem, Icon, Form, FormItem, Input, Select, Option, DatePicker, InputNumber},
	data () {
		return {
			title:'新建',
			showBox:false,
			listenerObj:{},
			
			ruleValidate: {
				nickname: [
					{ required: true, message: '请选择倾听者', trigger: 'blur' },
				],
				status: [
					{ required: true, message: '请填写状态', trigger: 'change', type:'number' }
				],
				sequence: [
					{ required: true, message: '请填写权重', trigger: 'change', type:'number' }
				],
			},
			
			editorOption:{
				placeholder:'请输入内容',
				modules:{
					imageResize: {},
					toolbar:{
                        container: [
							['bold', 'italic', 'underline'],        // toggled buttons
							[{ 'size': ['small', false, 'large', 'huge'] }],  // custom dropdown
							[{ 'header': [1, 2, 3, 4, 5, 6, false] }],

							[{ 'color': [] }, { 'background': [] }],          // dropdown with defaults from theme
							[{ 'font': fonts }],
							['clean'], 
                        ],
                    },
					syntax: {highlight:text => hljs.highlightAuto(text).value},


				}
			},
			vform:{
				status: 0,
				nickname: null,
				sequence:1,
				memo:'',
				uid:'',
				content:''
			},
			statusList: [
				{value: 0,label: '暂存'},
				{value: 1,label: '正常'},
				{value: 2,label: '关闭'},
			],
			
			vData:[]
		}
	},
	methods:{
		init:function(){},

		onSave:function(index){
			let self=this
			let _flag=true
			this.$refs.vform.validate((valid) => {
				if (!valid){
					this.$Message.error('请完成必填项')
					_flag=false
				}
			})
			if(!_flag){return}
			self.$store.dispatch("setSpinShow",true)
			self.$http.post("/v1/welisten/talks?token="+self.token,{'data':JSON.stringify(this.vform)})
			.then(function(response){
				if(200==response.data.status && 0==response.data.code)
				{
					self.$Message.success('操作成功')
					self.$router.push({path:'/welisten/talk/edit/'+response.data.data.id})
				}else{
					self.$Message.error(response.data.message)
				}
				self.$store.dispatch("setSpinShow",false)
			}).catch(function(error){
				self.$Message.error(error.message)
				self.$store.dispatch("setSpinShow",false)
			})
		},
		onSelectUser:function(){
			this.showBox=true
		},
		onListenerChange:function(obj){
			this.listenerObj=obj
			this.vform.uid=this.listenerObj.uid
			this.vform.nickname=this.listenerObj.nickname
		},
	},
	created:function(){
	},
	mounted () {
		this.init()
    	this.setTitle(this.title)
	},
	activated:function(){
	},
	computed:{
		...mapGetters(['token','menuIndex'])
	},
}



</script>

<style lang="less" scoped>
</style>